package com.smy.common;
import java.sql.Connection;
import java.util.Collection;
import java.util.List;
public interface MyService<T> {
    default Bool<T> boolQuery(Bool<T> bool){
        Connection c = BaseDao.getConnection();
        try {
            return this.getDao().boolQuery(bool,c);
        } catch (Exception e){
            e.printStackTrace();;
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(c,null,null);
        }
    }
    default Bool<T> boolQuery(Bool<T> bool,Connection c){
        try {
            return this.getDao().boolQuery(bool,c);
        } catch (Exception e){
            e.printStackTrace();;
            throw new RuntimeException(e);
        }
    }
    default int save(T t) {
        Connection c = BaseDao.getConnection();
        try {
            return this.getDao().insert(t,c);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(c,null,null);
        }
    }
    default int save(T t,Connection c) {
        try {
            return this.getDao().insert(t,c);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    default boolean saveBatch(Collection<T> tList,Connection c){return true;};
    default List<T> list(Connection c){
        try {
            return this.getDao().selectAll(c);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    default List<T> list(){
        Connection c = BaseDao.getConnection();
        try {
            return this.getDao().selectAll(c);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(c,null,null);
        }
    }
    default T getById(int id,Connection c){
        try {
            return this.getDao().selectById(id,c);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    default T getById(int id){
        Connection c = BaseDao.getConnection();
        try {
            return this.getDao().selectById(id,c);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(c,null,null);
        }
    }
    default int update(T t,Connection c){
        try {
            return this.getDao().updateById(t,c);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    default int update(T t){
        Connection c = BaseDao.getConnection();
        try {
            return this.getDao().updateById(t,c);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(c,null,null);
        }
    }
    default int removeById(T t,Connection c){
        try {
            return this.getDao().deleteById(t,c);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    default int removeById(T t){
        Connection c = BaseDao.getConnection();
        try {
            return this.getDao().deleteById(t,c);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(c,null,null);
        }
    }
    MyDao<T> getDao();
}
